package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.Image;
import com.kgd.news.entity.Media;
import com.kgd.news.utils.Api;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.DateUtils;
import com.kgd.news.utils.StringUtilsWx;

import java.sql.SQLException;
import java.util.List;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/10/9
 * @Time: 下午3:03
 */
public class ImageController extends BaseController {

    @Override
    public void list() {
        String title = getPara("title");
        String categoryid = getPara("categoryid");
        String sql = "from image where 1 = 1 ";
        String type = "image";
        if (!StringUtilsWx.isEmpty(title)) {
            sql += " and title like '%" + title + "%'";
            type += title;
        }
        if (!StringUtilsWx.isEmpty(categoryid)) {
            if ("-1".equals(categoryid)) {
                sql += " and categoryid is null" ;
            } else {
                sql += " and categoryid =" + categoryid;
            }
            type += categoryid;
        }
        sql += " order by id desc";
        Page<Image> data = Image.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        setAttr("categoryid", categoryid);
        render("image_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        Image img = Image.dao.findById(id);
        setAttr("categoryid", getPara("categoryid"));
        setAttr("image", img);
        render("image_edit.html");
    }

    @Override
    public void save() {
        UploadFile uploadFile = getFile("img");
        String filePath = "";
        if (uploadFile != null && !StringUtilsWx.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        Image us = getModel(Image.class, "image");
        if (!StringUtilsWx.isEmpty(filePath)) {
            us.setCover(filePath);
        }
        if (us != null && us.getId() != null) {
            us.update();
        } else {
            us.setCreatetime(DateUtils.getCurDateStr());
            us.save();
        }
//        final long imgId = us.getId();
//        final String ids = getPara("img_ids");
//        if (!StringUtilsWx.isEmpty(ids)) {
//            Db.tx(new IAtom() {
//                @Override
//                public boolean run() throws SQLException {
//                    String id = ids.substring(0, ids.length() - 1);
//                    if (!StringUtilsWx.isEmpty(id)) {
//                        String sql = "update media set otherid = " + imgId + " where id in (" + id + ")";
//                        int result = Db.update(sql);
//                        return result > 0;
//                    }
//                    return false;
//                }
//            });
//        }

//        final   String addresses = getPara("img_addresses");
//        if (!StringUtilsWx.isEmpty(addresses)) {
//            Db.tx(new IAtom() {
//                @Override
//                public boolean run() throws SQLException {
//                    String[] addressArray = addresses.split("\\n");
//                    if (addressArray.length > 0) {
//                        for (int i = 0; i < addressArray.length; i++) {
//                            String addr = addressArray[i];
//                            if (!StringUtilsWx.isEmpty(addr)) {
//                                Media m = new Media();
//                                m.setImg(addr.trim());
//                                m.setOtherid(imgId);
//                                m.setType(1);
//                                if (m.save()) {
//                                    long id=  m.getId();
//                                }
//                            }
//                        }
//                        return true;
//                    }
//                    return false;
//                }
//            });
//        }
        setAttr("categoryid", getPara("categoryid"));
        CacheKit.removeAll("image");
        CacheKit.removeAll("imageAndVideo");

        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from image where id in (" + ids + ")");
        Db.update("delete from media where otherid in (" + ids + ")");
        setAttr("categoryid", getPara("categoryid"));
        CacheKit.removeAll("image");
        list();
    }

    public void uplodImg(){
        UploadFile uploadFile = getFile();
        String filePath = "";
        if (uploadFile != null && !StringUtilsWx.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        Media m = new Media();
        m.setImg(filePath);
        m.setType(1);
        if (m.save()){
            renderJson(new Api(Api.S_200, m).get());
        }else{
            renderJson(new Api(Api.E_201, "'").get());
        }
    }

    public void imageList(){
        String imageid = getPara("imageid");
        if (imageid != null) {
            getSession().setAttribute("imageid", imageid);
        } else {
            imageid = (String) getSession().getAttribute("imageid");
        }
        List<Media> list = Media.dao.find("select * from media where type = 1 and otherid = ?", imageid);
//        其实都设置给了，request里面了吧。
        setAttr("categoryid", getPara("categoryid"));
        setAttr("data", list);
        setAttr("imageid", imageid);
        render("media_list.html");
    }
    public void savePic() {
        Media media = getModel(Media.class, "media");
        if (media != null && media.getId() != null) {
            media.update();
            imageList();
        }
    }

    public void editPic() {
        Integer id = getParaToInt("id");
        Media media = Media.dao.findById(id);
        setAttr("pic", media);
        render("pic_edit.html");
    }
    public void delPic(){
        String ids = getPara("ids");
        Db.update("delete from media where id in (" + ids + ")");

        imageList();
    }
}
